package com.employeemanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.employeemanage.domain.bean.TblEmployee;
import com.employeemanage.domain.resp.PageResult;
import com.employeemanage.domain.vo.EmployeeAddVo;
import com.employeemanage.domain.vo.EmployeeListVo;
import com.employeemanage.domain.vo.EmployeeUpdateVo;
import com.employeemanage.domain.vo.EmployeePerformanceListVo;
import com.employeemanage.domain.vo.EmployeePerformanceResponseVo;
import com.employeemanage.domain.vo.EmployeePerformanceUpdateVo;
import com.employeemanage.mapper.TblEmployeeMapper;
import com.employeemanage.service.TblEmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
* @author 郭可欣
* @description 针对表【tbl_employee(员工表)】的数据库操作Service实现
* @createDate 2025-07-05 14:15:33
*/
@Service
public class TblEmployeeServiceImpl extends ServiceImpl<TblEmployeeMapper, TblEmployee>
    implements TblEmployeeService {

    @Override
    public PageResult<TblEmployee> getEmployeeList(EmployeeListVo employeeListVo) {
        // 创建分页对象
        Page<TblEmployee> page = new Page<>(employeeListVo.getPageNum(), employeeListVo.getPageSize());
        
        // 创建查询条件
        LambdaQueryWrapper<TblEmployee> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        if (StringUtils.hasText(employeeListVo.getName())) {
            queryWrapper.like(TblEmployee::getName, employeeListVo.getName());
        }
        if (StringUtils.hasText(employeeListVo.getPosition())) {
            queryWrapper.eq(TblEmployee::getPosition, employeeListVo.getPosition());
        }
        if (StringUtils.hasText(employeeListVo.getStatus())) {
            queryWrapper.eq(TblEmployee::getStatus, employeeListVo.getStatus());
        }
        
        // 按ID降序排序
        queryWrapper.orderByDesc(TblEmployee::getId);
        
        // 执行分页查询
        Page<TblEmployee> resultPage = this.page(page, queryWrapper);
        
        // 返回分页结果
        return new PageResult<>(resultPage);
    }

    @Override
    public Long addEmployee(EmployeeAddVo employeeAddVo) {
        // 创建员工实体
        TblEmployee employee = new TblEmployee();
        BeanUtils.copyProperties(employeeAddVo, employee);
        
        // 设置默认值
        employee.setPerformance(new java.math.BigDecimal("0.00"));
        employee.setWorkScore(new java.math.BigDecimal("0.00"));
        employee.setStatus("在职");
        employee.setCreatedAt(new Date());
        employee.setUpdatedAt(new Date());
        
        // 保存员工信息
        this.save(employee);
        
        return employee.getId();
    }

    @Override
    public boolean updateEmployee(EmployeeUpdateVo employeeUpdateVo) {
        // 根据ID查询员工
        TblEmployee employee = this.getById(employeeUpdateVo.getId());
        if (employee == null) {
            return false;
        }
        
        // 更新员工信息
        BeanUtils.copyProperties(employeeUpdateVo, employee);
        employee.setUpdatedAt(new Date());
        
        // 保存更新
        return this.updateById(employee);
    }

    @Override
    public boolean deleteEmployee(Long id) {
        // 根据ID删除员工
        return this.removeById(id);
    }

    @Override
    public PageResult<EmployeePerformanceResponseVo> getEmployeePerformanceList(EmployeePerformanceListVo performanceListVo) {
        Page<TblEmployee> page = new Page<>(performanceListVo.getPageNum(), performanceListVo.getPageSize());
        LambdaQueryWrapper<TblEmployee> queryWrapper = new LambdaQueryWrapper<>();

        // 查询条件
        if (performanceListVo.getEmployeeId() != null) {
            queryWrapper.eq(TblEmployee::getId, performanceListVo.getEmployeeId());
        }
        if (performanceListVo.getMinScore() != null) {
            queryWrapper.ge(TblEmployee::getPerformance, performanceListVo.getMinScore());
        }
        if (performanceListVo.getMaxScore() != null) {
            queryWrapper.le(TblEmployee::getPerformance, performanceListVo.getMaxScore());
        }

        // 支持姓名模糊查找（如果VO有name字段）
        try {
            java.lang.reflect.Field nameField = performanceListVo.getClass().getDeclaredField("name");
            nameField.setAccessible(true);
            Object nameValue = nameField.get(performanceListVo);
            if (nameValue != null && org.springframework.util.StringUtils.hasText(nameValue.toString())) {
                queryWrapper.like(TblEmployee::getName, nameValue.toString());
            }
        } catch (Exception ignored) {}

        queryWrapper.orderByDesc(TblEmployee::getPerformance);

        Page<TblEmployee> resultPage = this.page(page, queryWrapper);

        java.util.List<EmployeePerformanceResponseVo> voList = new java.util.ArrayList<>();
        for (TblEmployee emp : resultPage.getRecords()) {
            EmployeePerformanceResponseVo vo = new EmployeePerformanceResponseVo();
            vo.setId(emp.getId());
            vo.setName(emp.getName());
            vo.setPosition(emp.getPosition());
            vo.setOrderCount(0); // 暂无订单统计
            vo.setSalesAmount(java.math.BigDecimal.ZERO); // 暂无销售额统计
            vo.setPerformance(emp.getPerformance()); // 绩效总分
            vo.setWorkScore(emp.getWorkScore()); // 工作态度得分
            voList.add(vo);
        }
        return new PageResult<>(voList, resultPage.getTotal());
    }

    @Override
    public boolean updateEmployeePerformance(EmployeePerformanceUpdateVo performanceUpdateVo) {
        TblEmployee employee = this.getById(performanceUpdateVo.getId());
        if (employee == null) {
            return false;
        }
        if (performanceUpdateVo.getPerformance() != null) {
            employee.setPerformance(performanceUpdateVo.getPerformance());
        }
        if (performanceUpdateVo.getWorkScore() != null) {
            employee.setWorkScore(performanceUpdateVo.getWorkScore());
        }
        employee.setUpdatedAt(new java.util.Date());
        return this.updateById(employee);
    }

    @Override
    public boolean deleteEmployeePerformance(Long id) {
        TblEmployee employee = this.getById(id);
        if (employee == null) {
            return false;
        }
        employee.setPerformance(java.math.BigDecimal.ZERO);
        employee.setWorkScore(java.math.BigDecimal.ZERO);
        employee.setUpdatedAt(new java.util.Date());
        return this.updateById(employee);
    }

    @Override
    public TblEmployee login(TblEmployee account) {
        TblEmployee employee = this.getById(account.getId());
        if (employee == null||!employee.getPassword().equals(account.getPassword())) {
            throw new RuntimeException("用户或密码错误");
        }
        return employee;


    }
}




